<?php		
/**
 * 
 */

class InstallFace extends FaceBase{


	private $api = null;
	/**
	 * 构造函数，初始化插件
	 */
	public function __construct() {
		parent::__construct();
		
		//将标准头加进来，在析构函数中加了标准尾。对于风格的变化，只需要调整标准头标准尾
		session_start();
		$this->api = n_api("install");
	}

	public function login(){
		
		$this->api->isInstall();
		$this->_html("login");
	}

	public function dologin(){
		$param = $this->param("name:char,pwd:char");
		$param['pwd'] = md5('n'.$param['pwd']);
		
		try{
			$conf = n_confGet("install");
			
			if(!$conf['state']){
				throw new PException("配置文件丢失!");
			}
			if($conf['data']['name']!=$param['name']){
				throw new PException("账号错误!");
			}
			if($conf['data']['pwd']!=$param['pwd']){
				throw new PException("密码错误!");
			}
			
			$_SESSION['islogin'] = 'Y';
			$rs = n_state(TRUE, "登录成功");
		}catch(CException $c){
			$rs = n_state(false, $p->getMessage());

		}catch(PException $p){
			$rs = n_state(false, $p->getMessage());
		}catch(exception $e){
			$rs = n_state(false, $p->getMessage());
			
		}
		echo json_encode($rs);
	}
	

	public function logout(){
		$_SESSION['islogin'] = 'N';
		n_go(n_url("install","login"));
	}
	public function init(){
		//生成用户配置文件
		
		//生成内核配置文件
		//init
		//ini
		$this->_html("header");
		$this->_html("init");	
		$this->_html("footer");
		
	}
	
	
	public function initStep1(){
		
		$this->_html("header");
		$this->_html("initStep1",$_SESSION);
		$this->_html("footer");
	}
	public function initStep2(){
		$this->_html("header");
		$this->_html("initStep2",$_SESSION);
		$this->_html("footer");
	}
	public function initPreview(){
		
		
		$rs['item'][] = "管理员账号:{$_SESSION['core_name']}";
		$rs['item'][] = "密码:{$_SESSION['core_pwd']}";
		$rs['item'][] = "域名:{$_SESSION['core_domain']}";

		$this->_html("header");
		$this->_html("initPreview",$rs);
		$this->_html("footer");

	}
	public function initSave(){
		
		foreach($_SESSION as $key=>$val){
			if('core_'!=substr($key,0, 5)){
				continue;
			}
			$rs[substr($key,5)] = $val;
		}
		//echo N_DATA;
		//mkdir(N_DATA);
		//mkdir(N_LOG);
		if(empty($rs['pwd']) || empty($rs['name']) || empty($rs['repwd'])){
			throw new PException("请完整填写登录信息");
		}
		if($rs['pwd']!=$rs['repwd']){
			throw new PException("输入的密码不一致");
		}
		
		$pwd = md5('n'.$rs['pwd']);
		
		
		if(empty($rs['domain'])){
			throw new PException("请填写域名");
		}
		
		$core['domain'] = $rs['domain'];
		$core['burl'] = 'N';
		$core['confType'] = 'dev';
		$core['datapath'] = 'data';
		$core['logpath'] = 'log';
		$core['loglevel'] = 'false,notice,ufo';
		$core['defaultPlugin'] = 'ndef';
		$core['def404'] = 'ndef/def404';
		$core['def500'] = 'ndef/def500';
		$core['other'] = '';
		
		n_confSave('install', array('name'=>$rs['name'],'pwd'=>$pwd));
		n_confSave('_core', $core);
		n_confSave("_plugins", array("install"=>array('name'=>'安装','pevent'=>"install",'descript'=>'提供插件安装功能','version'=>'0.01',"ctime"=>time(),"state"=>'start')));
		
		$this->_html("header");
		$this->_html("initSave");
		$this->_html("footer");

	}
	public function initCache(){
		unset($_GET['event']);
		unset($_GET['action']);
		if(!empty($_GET)){
		foreach($_GET as $key=>$val){
			$_SESSION['core_'.$key] = $val;
		}
		}
		echo json_encode(n_state(TRUE, "保存成功"));
	}
	
	
	

	public function index(){
		//echo '===';
		$this->api->islogin();
		$rs = $this->api->getListByTags("test");
		$pconf = n_confGet("_plugins");
		
		$rs['installed'] = $pconf['data'];
		$this->_html("header");
		$this->_html("index",$rs);
		$this->_html("footer");
	}
	public function installed(){
		$this->api->islogin();
		$pconf = n_confGet("_plugins");
		
		$rs['installed'] = $pconf['data'];
		$this->_html("header");
		$this->_html("installed",$rs);
		$this->_html("footer");
		
	}
	
	public function pluginInit(){
		$param = $this->param("pcode:char");
		$url = "http://10.10.126.109/open/pbox/info.html?scode=".N_SCODE."&pcode={$param['pcode']}";
		
		$rs = $this->api->curlGet($url,0,'m3.sohu.com');
		
		$this->_html("header");
		$this->_html("pluginInit",json_decode($rs['respond'],true));
		$this->_html("footer");

	}
	public function pluginDownload(){
		$param = $this->param("pcode:char");
		$param['pcode'] = strtolower($param['pcode']);

		try{
			$url = "http://10.10.126.109/open/pbox/download.html?scode=".N_SCODE."&pcode={$param['pcode']}";
			$savepath = N_DATA."/install/{$param['pcode']}.zip";
			$dlFile = $this->api->download($url,$savepath,'m3.sohu.com');
			if(!$dlFile['state']){
				throw new PException("下载失败");
			}
			$zip = new ZipArchive();
			if(TRUE != $zip->open($savepath)){
				throw new PException("打开文件失败");
			}
			$pluginPath = N_PLUGINS."/{$param['pcode']}";
			if(!is_dir(dirname($pluginPath))){
				mkdir($pluginPath);
			}
			$zip->extractTo($pluginPath);
			$zip->close();
			$rs = n_state(TRUE, "下载并解包成功！下载地址:{$pluginPath}");

		}catch(PException $p){
			$rs = n_state(FALSE, $p->getMessage());
		}
		$rs['pevent'] = $param['pcode'];

		$this->_html("header");
		$this->_html("pluginDownload",$rs);
		$this->_html("footer");
	}
	public function pluginCheck(){
		$param = $this->param("pcode:char");
		try{

			$url = "http://10.10.126.109/open/pbox/nexus.html?scode=".N_SCODE."&pcode={$param['pcode']}";
			$prs = $this->api->curlGet($url,0,'m3.sohu.com');
			$nexus = json_decode($prs['respond'],true);
			//print_r($nexus);
			$rs['check'][] = $this->api->checkOs($nexus['os']);
			$rs['check'][] = $this->api->checkPhpv($nexus['phpv']);
			$rs['check'][] = $this->api->checkExt($nexus['ext']);
			$rs['check'][] = $this->api->checkNexus($nexus['nexus'],'Y');
		}catch(CException $c){
			$rs[] = "该插件未定义安装脚本，系统将自动解包，并按预定规则部署代码";
		}
		$rs['pevent'] = $param['pcode'];
		$this->_html("header");
		$this->_html("pluginCheck",$rs);
		$this->_html("footer");
	}
	public function pluginSetup(){
		$param = $this->param("pcode:char");
		
		try{
			
			$pHandle = n_api($param['pcode']);
			$inrs = $pHandle->installSetup();
			$pConf = n_confGet("_plugins");
			if(!$pConf['state']){
				throw new PException("配置文件错误");
			}
			
			$newConf = $pConf['data'];

			$url = "http://10.10.126.109/open/pbox/info.html?scode=".N_SCODE."&pcode={$param['pcode']}";
			
			$prs = $this->api->curlGet($url,0,'m3.sohu.com');
			$pinfo = json_decode($prs['respond'],true);
			
			$newConf[$param['pcode']] = array(
											'pevent'=>$pinfo['pevent'],
											'name'=>$pinfo['pname'],
											'descript'=>$pinfo['descript'],
											'version'=>$pinfo['version'],
											'ctime'=>N_NOW
			);
			n_confSave("_plugins", $newConf);
			$this->api->pluginStart($param['pcode']);
			$rs = n_state(TRUE, "安装成功");
		}catch(PException $p){
			$rs = n_state(FALSE, $p->getMessage());
		}
		$rs['inrs'] = $inrs;
		
		$this->_html("header");
		$this->_html("pluginSetup",$rs);
		$this->_html("footer");

	}
	public function pluginStop(){
		$param = $this->param("pcode:char");

		$pHandle = n_api($param['pcode']);
		
		//停止服务
		$this->api->pluginStop($param['pcode']);

		$rs = array();

		$this->_html("header");
		$this->_html("pluginStop",$rs);
		$this->_html("footer");

		
	}

	public function pluginStart(){
		$param = $this->param("pcode:char");

		$pHandle = n_api($param['pcode']);
		
		//停止服务
		$this->api->pluginStart($param['pcode']);

		$rs = array();

		$this->_html("header");
		$this->_html("pluginStart",$rs);
		$this->_html("footer");

		
	}

	public function pluginRemove(){
		$param = $this->param("pcode:char");

		$pHandle = n_api($param['pcode']);
		
		//停止服务
		$this->api->pluginStop($param['pcode']);
		
		//数据
		//缓存
		$inrs = $pHandle->installRemove();
		
		//配置文件
		$this->api->pluginRemove($param['pcode']);
		//unlink(N_CONF . "/{$param['pcode']}");
		
		//代码
		$today = date("Ymd");
		$pluginPath = N_PLUGINS ."/{$param['pcode']}";
		$newPath = $pluginPath."_remove{$today}";
		$i = 1;
		while(true){
			if($i>1){
				$tmpPath = $newPath."_{$i}";
			}else{
				$tmpPath = $newPath;
			}
			if(is_dir($tmpPath)){
				$i++;
				continue;
			}
			rename($pluginPath,$tmpPath);
			break;
		}
		

		$rs = array();
		$this->_html("header");
		$this->_html("pluginRemove",$rs);
		$this->_html("footer");		
	}

	public function coreConfEdit(){
		$rs = n_confGet("_core");
		if(!$rs['state']){
			throw new PException("找不到配置文件");
		}
		$this->_html("header");
		$this->_html("coreConfEdit",$rs['data']);
		$this->_html("footer");		
	}
	/**
	 * 
	 */
	public function coreConfSave(){
		$param = $this->param("isBurl:char,
								confType:char,
								datapath:char,
								logpath:char,
								isbackupUninstall:char,
								loglevel:uint,
								defaultPlugin:char,
								default404:char,
								default500:char
								");
		
		
		$rs =n_confSave("_core",$data);
		$this->_html("coreConfSave",$rs);
	}
	
	public function installConfEdit(){
		$rs = n_confGet("install");
		if(!$rs['state']){
			throw new PException("找不到配置文件");
		}
		
		$this->_html("header");
		$this->_html("installConfEdit",$rs['data']);
		$this->_html("footer");
		
	}
	public function installConfSave(){
		$param = $this->param("admin:char,
								pwd:char
								");
		
		
		$rs =n_confSave("_core",$data);
		$this->_html("installConfSave",$rs);
		
	}
	

	public function __destruct(){
		//加入标准尾。
	}
}

